<?php

/**
 * Created by PhpStorm.
 * User: 志俊
 * Date: 2015/5/28 0028
 * Time: 12:29
 */
class Keywords_model extends CI_Model
{

    var $Id = '';
    var $badword = '';

    function __construct()
    {
        parent::__construct();
    }

    //从文本导入敏感词
    function input_badword($file_name)
    {
        header("Content-type: text/html; charset=utf-8");
        $file = file($file_name);
        $i = 0;
        $j = 0;
        foreach ($file as $line) {
            ++$i;
            $this->db->set('badword', $line);
            $bool = $this->db->insert('keywords');
            if ($bool) {
                ++$j;
            } else {
                $error[$j] = $line;
            }
        }
        if ($i == $j) {
            return 1;
        } else {
            //数据异常
            $system_blog = array(
                'text' => '添加敏感词，记录失败。增加的敏感词为：' . var_dump($error) . ',sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }

    //增加敏感词
    function add_badword($badword_name)
    {
        if (!empty($badword_name)) {
            $data['badword'] = $badword_name;
            $bool = $this->db->insert('keywords', $data);
            if ($bool) {
                //更新成功
                return $this->db->insert_id();
            } else {
                //数据异常
                $system_blog = array(
                    'text' => '添加敏感词，记录失败。增加的敏感词为：' . $badword_name . ',sql:' . $this->db->last_query(),
                    'date' => date('Y-m-d')
                );
                $this->db->insert('system_blog', $system_blog);
                return -1;
            }
        } else {
            //非法操作
            $system_blog = array(
                'text' => '添加敏感词，非法操作。badword值为NULL',
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }

    //显示敏感词列表
    function show_keywords_list($page, $limit)
    {
        $result = $this->db->from('keywords')->order_by('Id', 'ASC')->limit($limit, $page * $limit)->distinct()->get()->result_array();
        return array(
            'total' => $this->db->count_all_results('keywords'),
            'data' => $result,
            'data_num' => count($result)
        );
    }

    //删除敏感词
    function delete_keywords($Id)
    {
        if (!empty($Id)) {
            $bool = $this->db->delete('keywords', array('Id' => $Id));
            if ($bool) {
                //删除成功
                return 1;
            } else {
                //数据异常
                $system_blog = array(
                    'text' => '删除敏感词，记录失败。标签id为' . $Id . ',sql:' . $this->db->last_query(),
                    'date' => date('Y-m-d')
                );
                $this->db->insert('system_blog', $system_blog);
                return -1;
            }
        } else {
            //非法操作
            $system_blog = array(
                'text' => '删除敏感词，非法操作。Id值为NULL',
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -2;
        }
    }

    //去重敏感词
    function distinct_keywords()
    {
        $this->load->dbforge();
        $this->db->query("create table IF NOT EXISTS keyword_tmp1 as (select Min(Id) as maxid from keywords group by badword having COUNT(Id)>1)");
        $this->db->query("create table IF NOT EXISTS keyword_tmp2 as (select Id from keywords group by badword having COUNT(Id)>1)");
        $bool = $this->db->query("delete from keywords where Id not in (select maxid from keyword_tmp1) and Id in (select Id from keyword_tmp2)");
        $this->dbforge->drop_table('keyword_tmp1');
        $this->dbforge->drop_table('keyword_tmp2');
        if ($bool) {
            //更新成功

            return 1;
        } else {
            //数据异常
            $system_blog = array(
                'text' => '敏感词去重，记录失败。sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }

    //修改敏感词
    function update_keywords($Id, $text)
    {
        $data = array(
            'badword' => $text
        );
        $bool = $this->db->update('keywords', $data, array('Id' => $Id));
        if ($bool) {
            //更新成功
            return 1;
        } else {
            //数据异常
            $system_blog = array(
                'text' => '更新敏感词，记录失败。更新的id为' . $Id . ',更新敏感词为' . $text . 'sql:' . $this->db->last_query(),
                'date' => date('Y-m-d')
            );
            $this->db->insert('system_blog', $system_blog);
            return -1;
        }
    }
}